//
//  CTPersonalSignModifyView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/29.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTPersonalSignModifyView.h"
#import "CTHeader.h"
#import "CTDebugManager.h"
#import "UIViewAdditions.h"
#import "CTAppLanguage.h"
#import "CTTextManager.h"

@interface CTPersonalSignModifyView()<UITextViewDelegate>
{
    UIView          *topView;
    UITextView      *signTextView;
    UILabel         *signLabelNumber;
}

@end

@implementation CTPersonalSignModifyView

@synthesize delegate;

- (void)dealloc
{
    delegate = nil;
    [topView removeFromSuperview];
    topView = nil;
    [signTextView removeFromSuperview];
    signTextView = nil;
    [signLabelNumber removeFromSuperview];
    signLabelNumber = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTSetView Is Removed!!!"];
}


- (instancetype)init
{
    self = [super init];
    if (self)
    {
        
        self.frame = CGRectMake(0, 0,CT_HEADER_SCREEN_WIDTH,CT_HEADER_SCREEN_HEIGHT);
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureAction:)];
        [self addGestureRecognizer:tapGesture];
        [self createSetTitleView];
        [self createPersonalSignmodifyView];
    }
    return self;
}
//创建标题
- (void)createSetTitleView
{
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 64)];
    topView.backgroundColor = CT_HEADER_WHITE_COLOR;
    topView.alpha =0.98;
    [self addSubview:topView];
    
    //标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, CT_HEADER_SCREEN_WIDTH, 44)];
    titleLabel.text = [CTAppLanguage localizedStringForKey:@"personalSignModifyView_title_text" withValue:nil withTable:@"Language"];
    titleLabel.font = [UIFont systemFontOfSize:19.0f];
    titleLabel.textColor = CT_HEADER_GREEN_COLOR;
    
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:titleLabel];
    
    //返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE_CLICK] forState:UIControlStateHighlighted];
    [topView addSubview:backButton];
    
    UIButton *finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    finishButton.frame = CGRectMake(CT_HEADER_SCREEN_WIDTH-44,20 , 44, 44);
    [finishButton addTarget:self action:@selector(finishButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    finishButton.backgroundColor = [UIColor clearColor];
    [finishButton setTitle:[CTAppLanguage localizedStringForKey:@"userNickNameView_saveLabel_text" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
    [finishButton setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateNormal];
    [topView addSubview:finishButton];
    
    UILabel * topBottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, topView.height-0.5, CT_HEADER_SCREEN_WIDTH, 0.5)];
    topBottomLine.backgroundColor = CT_HEADER_DEEPLINE_COLOR;
    [topView addSubview:topBottomLine];
}

- (void)createPersonalSignmodifyView
{
    UILabel * viewTopLine = [[UILabel alloc]initWithFrame:CGRectMake(0, topView.bottom+20, CT_HEADER_SCREEN_WIDTH, 0.5)];
    viewTopLine.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
    [self addSubview:viewTopLine];
    
    signTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, viewTopLine.bottom, CT_HEADER_SCREEN_WIDTH, 130)];
    signTextView.textColor = CT_HEADER_BLACK_COLOR;
    signTextView.font = [UIFont systemFontOfSize:18];
    signTextView.bounces = NO;
    signTextView.text = [CTAppLanguage localizedStringForKey:@"personalSignModifyView_textView_text" withValue:nil withTable:@"Language"];
    signTextView.delegate = self;
    [self addSubview:signTextView];
    
    
    UILabel * viewBottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, signTextView.bottom, CT_HEADER_SCREEN_WIDTH, 0.5)];
    viewBottomLine.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
    [self addSubview:viewBottomLine];
    
    signLabelNumber = [[UILabel alloc]initWithFrame:CGRectMake(signTextView.width-10-90, viewBottomLine.origin.y-17-15, 90, 15)];
    signLabelNumber.text = @"0/40";
    signLabelNumber.font = [UIFont systemFontOfSize:15];
    signLabelNumber.textAlignment = NSTextAlignmentRight;
    signLabelNumber.textColor = [UIColor colorWithRed:203/255.0 green:203/255.0 blue:199/255.0 alpha:1.0];
    [self addSubview:signLabelNumber];
}
//回收键盘
-(void)tapGestureAction:(UITapGestureRecognizer *) sender
{
    [signTextView resignFirstResponder];
}
#pragma mark titleView_Action
- (void)backButtonAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(BackBtnAction:)])
    {
        [self.delegate BackBtnAction:self];
    }
}
- (void)finishButtonAction:(UIButton *)sender
{
    [signTextView resignFirstResponder];
    
//    if ([self.delegate respondsToSelector:@selector(personalSignModifyViewSelectedSaveBtnAction:withSexString:)])
//    {
//        if ([signTextView.text isEqualToString:[CTAppLanguage localizedStringForKey:@"personalSignModifyView_textView_text" withValue:nil withTable:@"Language"]])
//        {
//            if ([self.delegate respondsToSelector:@selector(BackBtnAction:)])
//            {
//                [self.delegate BackBtnAction:self];
//            }
//        }
//        else
//        {
//            
//            
//        }
//        
//        
//    }
    
    if ([self.delegate respondsToSelector:@selector(personalSignModifyViewSelectedSaveBtnAction:withSexString:)]) {
        [self.delegate personalSignModifyViewSelectedSaveBtnAction:self withSexString:signTextView.text];
    }
}

#pragma mark textViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:[CTAppLanguage localizedStringForKey:@"personalSignModifyView_textView_text" withValue:nil withTable:@"Language"]])
    {
        textView.text = nil;
    }
}
//如果输入超过规定的字数40，就不再让输入
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//    NSString *str = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//    if (str.length >=40)
//    {
//        return NO;
//    }
    //判断加上输入的字符，是否超过界限
    NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
    if (str.length > 40)
    {
        textView.text = [textView.text substringToIndex:40];
        return NO;
    }

    return YES;
}
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 40)
    {
        textView.text = [textView.text substringToIndex:40];
    }
    signLabelNumber.text = [NSString stringWithFormat:@"%d/40",(int)textView.text.length];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    CTTextManager *textManagre = [CTTextManager shareInstance];
    if ([textManagre isBlankString:textView.text] == YES)
    {
        textView.text = [CTAppLanguage localizedStringForKey:@"personalSignModifyView_textView_text" withValue:nil withTable:@"Language"];
    }
}

#pragma mark - setter -
-(void) setSignText:(NSString *)signStr
{
    signTextView.text = signStr;
    signLabelNumber.text = [NSString stringWithFormat:@"%d/40",(int)signStr.length];

}
@end

